package com.example.administrator.capital.home;

import android.app.Fragment;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.ViewFlipper;

import com.example.administrator.capital.MainFragment;
import com.example.administrator.capital.R;
import com.example.administrator.capital.adapter.CommentAdapter;
import com.example.administrator.capital.base.BaseMainFragment;
import com.example.administrator.capital.home.invite.InviteFragment;
import com.example.administrator.capital.item.ItemDetailFragment;
import com.example.administrator.capital.login.RegisterActivity;
import com.example.administrator.capital.mine.CenterFragment;
import com.example.administrator.capital.mine.HelpCenterFragment;
import com.example.administrator.capital.mine.second.InformationDisclosureFragment;
import com.example.administrator.capital.model.TestBean;
import com.example.administrator.capital.model.bean.HomeBean;
import com.example.administrator.capital.presenter.HomePresenter;
import com.example.administrator.capital.presenter.base.IViewRequest;
import com.example.administrator.capital.utils.JsonUtils;
import com.example.administrator.capital.utils.ToastUtil;
import com.lzy.okgo.model.Response;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import layout.LoadingLayout;
import recycle.BaseViewHolder;

/**
 * A simple {@link Fragment} subclass.
 * 首页
 */
public class HomeFragment extends BaseMainFragment {

    Unbinder unbinder;
    @BindView(R.id.recycle_view)
    RecyclerView mRecycleView;
    @BindView(R.id.swipe_layout)
    SwipeRefreshLayout mSwipeLayout;
    @BindView(R.id.loading)
    LoadingLayout mLoading;
    private RecyclerView mRecyclerViewTop;
    private RecyclerView mRecyclerViewBom;
    private CommentAdapter<HomeBean.DataBean.TuijianBean> mAdapter;
    private CommentAdapter<TestBean> mAdapterRecyclerViewTop;
    private CommentAdapter<HomeBean.DataBean.NewsBean> mAdapterRecyclerViewBom;
    private List<TestBean> mTestBeans = new ArrayList<>();
    private RelativeLayout mRegister;
    private HomePresenter mHomePresenter;
    private TextView mNew;
    public HomeFragment() {
        // Required empty public constructor
    }

    public static HomeFragment newInstance() {

        Bundle args = new Bundle();
        HomeFragment fragment = new HomeFragment();
        fragment.setArguments(args);
        return fragment;
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_home, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }
    @Override
    protected void initView(View view) {
        super.initView(view);
        mHomePresenter = new HomePresenter(null);
        mSwipeLayout.setColorSchemeResources(R.color.font_blue);
        mAdapter = new CommentAdapter<HomeBean.DataBean.TuijianBean>(R.layout.items_list,null) {
            @Override
            public void setViewData(BaseViewHolder holder, HomeBean.DataBean.TuijianBean item, int position) {
                holder.setText(R.id.home_title,item.getTitle())
                        .setProgress(R.id.progress_bar,item.getProgress())
                        .setText(R.id.percent,item.getProgress()+"%")
                        .setText(R.id.rate,item.getRate()+"%")
                        .setText(R.id.timelimit,item.getRefuNumber()+"")
                        .setText(R.id.fundamount,item.getFundamount()/1000000+"");
            }
            @Override
            public void setEvent(BaseViewHolder holder, final HomeBean.DataBean.TuijianBean item, int position) {
                holder.setOnClickListener(R.id.layout, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        ((MainFragment) getParentFragment()).startBrotherFragment(ItemDetailFragment.newInstance(String.valueOf(item.getProid())));
                    }
                });
            }
        };
        mRecycleView.setLayoutManager(new LinearLayoutManager(getContext()));
        DividerItemDecoration div = new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL);
        div.setDrawable(ContextCompat.getDrawable(getContext(), R.drawable.custom_divider_10));
        mRecycleView.addItemDecoration(div);
        mRecycleView.setAdapter(mAdapter);
    }
    @Override
    protected void setListener() {
        super.setListener();

        mSwipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                getData();
            }
        });
        mLoading.setRetryListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getData();
            }
        });
    }
    @Override
    protected void setData() {
        super.setData();
        addHeadView();
        getData();
    }
    private void getData() {
        mTestBeans.clear();
        mTestBeans.add(new TestBean(R.drawable.icon_01,"信息披露"));
        mTestBeans.add(new TestBean(R.drawable.icon_02,"安全保障"));
        mTestBeans.add(new TestBean(R.drawable.icon_03,"众望业务"));
        mTestBeans.add(new TestBean(R.drawable.icon_04,"邀请好友"));
        mAdapterRecyclerViewTop.setNewData(mTestBeans);
        mHomePresenter.homePage(new IViewRequest<String>() {
            @Override
            public void onFailMsg(String var) {
                ToastUtil.showShort(var);
            }
            @Override
            public void onCode(int code) {
                ToastUtil.showShort(code);
                mLoading.setErrorImage(R.drawable.error);
                mLoading.showError();
            }
            @Override
            public void onSuccess(Response<String> data) {
                HomeBean bean = JsonUtils.parse(data.body(), HomeBean.class);
                mAdapter.setNewData(bean.getData().getTuijian());
                mAdapterRecyclerViewBom.setNewData(bean.getData().getNews());
                mSwipeLayout.setRefreshing(false);
                mLoading.showContent();
            }
            @Override
            public void onFail(Response<String> error) {
            }
        });
    }
    /**
     * 添加head
     */
    private void addHeadView() {
        View headView = getLayoutInflater().inflate(R.layout.header_home, (ViewGroup) mRecycleView.getParent(), false);
        mNew = headView.findViewById(R.id.news);
        mRegister = headView.findViewById(R.id.ll_main_banner);
        mRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showActivity(RegisterActivity.class);
            }
        });
        mNew.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ((MainFragment) getParentFragment()).startBrotherFragment(CenterFragment.newInstance());
            }
        });
        mRecyclerViewBom = headView.findViewById(R.id.recycler_view_bom);
        mRecyclerViewTop = headView.findViewById(R.id.recycler_view_top);
        ViewFlipper viewFlipper = headView.findViewById(R.id.view_flipper);
        viewFlipper.startFlipping();
        mAdapterRecyclerViewTop = new CommentAdapter<TestBean>(R.layout.item_home_top,null) {
            @Override
            public void setViewData(BaseViewHolder holder, TestBean item, int position) {
                holder.setGlideLoadImageView(R.id.iv_classifcation_img, item.getId()).setText(R.id.tv_classifcation_text, item.getName());
            }
            @Override
            public void setEvent(BaseViewHolder holder, TestBean item, final int position) {
                holder.setOnClickListener(R.id.iv_classifcation_img, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (position==0) {
                            ((MainFragment) getParentFragment()).startBrotherFragment(InformationDisclosureFragment.newInstance());
                        } else if (position==1) {
                            ((MainFragment) getParentFragment()).startBrotherFragment(InsuranceFragment.newInstance());

                        } else if (position==2) {
                            ((MainFragment) getParentFragment()).startBrotherFragment(SomeBusinessFragment.newInstance());

                        } else if (position==3) {
                            ((MainFragment) getParentFragment()).startBrotherFragment(InviteFragment.newInstance());
                        }
                    }
                });

            }
        };
        mRecyclerViewTop.setLayoutManager(new GridLayoutManager(getActivity(), 4));
        mRecyclerViewTop.setAdapter(mAdapterRecyclerViewTop);
        mAdapterRecyclerViewBom = new CommentAdapter<HomeBean.DataBean.NewsBean>(R.layout.items_list,null) {
            @Override
            public void setViewData(BaseViewHolder holder, HomeBean.DataBean.NewsBean item, int position) {
                holder.setText(R.id.home_title,item.getTitle())
                        .setProgress(R.id.progress_bar,item.getProgress())
                        .setText(R.id.percent,item.getProgress()+".0%")
                        .setText(R.id.rate,item.getRate()+"%")
                        .setText(R.id.timelimit,item.getRefuNumber()+"")
                        .setText(R.id.fundamount,item.getFundamount()/1000000+"");
            }

            @Override
            public void setEvent(BaseViewHolder holder, final HomeBean.DataBean.NewsBean item, int position) {
                holder.setOnClickListener(R.id.layout, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        ((MainFragment) getParentFragment()).startBrotherFragment(ItemDetailFragment.newInstance(String.valueOf(item.getProid())));
                    }
                });
            }
        };
        mRecyclerViewBom.setLayoutManager(new LinearLayoutManager(getContext()));
        DividerItemDecoration div = new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL);
        div.setDrawable(ContextCompat.getDrawable(getContext(), R.drawable.custom_divider));
        mRecyclerViewBom.addItemDecoration(div);
        mRecyclerViewBom.setAdapter(mAdapterRecyclerViewBom);
        mAdapter.addHeaderView(headView);
    }
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
